package com.yyk.yykywd.mvvm.ui

import android.content.Intent
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.*
import com.yyk.yykywd.base.NewBaseActivity
import com.yyk.yykywd.databinding.ActivityMeetingInvitationBinding
import com.yyk.yykywd.mvvm.adapter.MeetingAdapter1
import com.yyk.yykywd.mvvm.entity.MeetingBean
import com.yyk.yykywd.mvvm.view_model.MeetingInvitationViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_meeting_invitation.*
import kotlinx.android.synthetic.main.rl_bottom.*
import kotlinx.android.synthetic.main.title.*

class MeetingInvitationActivity : NewBaseActivity() {
    private lateinit var viewModel: MeetingInvitationViewModel
    private lateinit var meetingAdapter: MeetingAdapter1

    override fun initView() {
//        setBackArrow()
//        setTitle("会议邀约")

        viewModel = ViewModelProviders.of(this).get(MeetingInvitationViewModel::class.java)
        val binding: ActivityMeetingInvitationBinding = DataBindingUtil.setContentView(this, R.layout.activity_meeting_invitation)
        binding.lifecycleOwner = this
        binding.meetingInvitationViewModel = viewModel

        tv_title.text = "会议邀约"
        iv_back.setOnClickListener {
            finish()
        }

        tv_execute.text = "创建会议邀约"

        viewModel.type = intent.getStringExtra(TYPE)

        if (viewModel.type == FROM_BUSINESS_FOLLOW) {
            rl.visibility = View.GONE
        } else {
            rl.visibility = View.VISIBLE
        }

        meetingAdapter = MeetingAdapter1()
        with(rv) {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = meetingAdapter
            //NestedScrollView嵌套RecyclerView，滑动RecyclerView列表会出现强烈的卡顿感，设置该方法解决
            isNestedScrollingEnabled = false
        }
    }

    override fun onResume() {
        super.onResume()
        reloadData()
    }

    override fun setListener() {
        super.setListener()
        viewModel.mMeeting.observe(this, Observer<MeetingBean> {
            hideRefreshAndLoadMore(srl)
            meetingAdapter.submitList(viewModel.mMeetings)
        })

        meetingAdapter.setOnItemChildClickListener { position, view ->
            val id = viewModel.mMeetings[position].id
            val name = viewModel.mMeetings[position].meeting_name
            if (viewModel.type == FROM_WORK) {
                ToAnotherActivity.toMeetingInvitationDetailActivity(mActivity, id.toString())
            } else {
                val i = Intent();
                i.putExtra(M_ID, id.toString());
                i.putExtra(M_NAME, name);
                setResult(FOLLOW_CODE, i);
                finish();
            }
        }

        srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
                viewModel.getMeetings()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                reloadData()
            }
        })

        tv_execute.setOnClickListener {
            if (whetherHaveMeetingPermission()) {
                return@setOnClickListener
            }
            ToAnotherActivity.toCreateOrModifyMeetingInvitationActivity(mActivity, CREATE, "")
        }
    }

    private fun reloadData() {
        with(viewModel) {
            refresh(mMeetings)
            getMeetings()
        }
    }

}
